package com.ccb.lms.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.ccb.lms.dto.ReaderBorrowCopyBookAuthority;
import com.ccb.lms.dto.ReaderBorrowHistoryDto;

import com.ccb.lms.dto.ReaderBorrowHistoryDto;

import com.ccb.lms.model.Reader;
import com.ccb.lms.model.User;
import com.ccb.lms.util.BasePage;
import com.ibatis.sqlmap.client.SqlMapClient;

public class ReaderDAO implements IReaderDAO {

	private SqlMapClient sqlMapClient = null;
	
	public SqlMapClient getSqlMapClient() {
		return sqlMapClient;
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}

	public boolean isLogin(Reader reader) throws SQLException {
		// TODO Auto-generated method stub
		if (sqlMapClient.queryForObject("reader.checkReader", reader) != null) {
			return true;
		}

		return false;
	}

	public boolean insertReader(Reader reader) throws SQLException {
		// TODO Auto-generated method stub
		sqlMapClient.insert("reader.insertReader", reader);
		return true;
	}

	public boolean deleteReader(Integer readerNO) throws SQLException {
		// TODO Auto-generated method stub
		sqlMapClient.delete("reader.deleteReader", readerNO);
		return true;
	}

	public boolean updateReader(Reader reader) throws SQLException {
		// TODO Auto-generated method stub
		sqlMapClient.update("reader.updateReader", reader);
		return true;
	}

	@SuppressWarnings("unchecked")
	public List<Reader> getAllReaders() throws SQLException {
		// TODO Auto-generated method stub
		return sqlMapClient.queryForList("reader.getAllReaders");
	}

	public Reader getReaderByID(String readerID) throws SQLException {
		// TODO Auto-generated method stub		
		return (Reader) sqlMapClient.queryForObject("reader.getReaderByID", readerID);
	}

	public int getReaderCount() throws SQLException {
		// TODO Auto-generated method stub
		return (Integer) sqlMapClient.queryForObject("reader.recordCount");
	}

	public BasePage getBasePage(int currentPage, int showRecordNum)
			throws SQLException {
		// TODO Auto-generated method stub
		return new BasePage(currentPage, getReaderCount(), showRecordNum);
	}

	@SuppressWarnings("unchecked")
	public List<Reader> getReaderByPage(BasePage basePage) throws SQLException {
		// TODO Auto-generated method stub

		return sqlMapClient.queryForList("reader.getReaderByPage", basePage);
	}


	@SuppressWarnings("unchecked")
	public List<ReaderBorrowCopyBookAuthority> getReaderCurrentBorrowDetails(
			int readerNO) throws SQLException {
		// TODO Auto-generated method stub
	
		return sqlMapClient.queryForList("reader.getReaderCurrentBorrowDetails", readerNO);
	}


	@SuppressWarnings("unchecked")
	public ArrayList<ReaderBorrowHistoryDto> getReaderBorrowHistoryDtosByreaderNO(
			int readerNO) {
		
		ArrayList<ReaderBorrowHistoryDto> ReaderBorrowHistoryDtos = null;
		
		try {
			ReaderBorrowHistoryDtos = (ArrayList<ReaderBorrowHistoryDto>) sqlMapClient.queryForList("reader.getReaderBorrowHistory",readerNO);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ReaderBorrowHistoryDtos;
	}	

}
